﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;
using System.Web;

namespace Portal.CMS
{
    public class UploadImageForGroupNews : WebPart
    {
      
        public override string WebPartId
        {
            get
            {
                return "UploadImageForGroupNews";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Hình đại diện");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        System.Web.UI.WebControls.Button SaveButton = null;
        System.Web.UI.WebControls.Label labelError = null;
        public override void LoadSubControls(System.Web.UI.WebControls.PlaceHolder pl, System.Web.UI.Page Page, LoadParamCls LoadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UploadImageForGroupNews));

            labelError = new System.Web.UI.WebControls.Label();
            labelError.CssClass = "ErrorUploadNewsImage";
            SaveButton = new System.Web.UI.WebControls.Button();
            SaveButton.Text = ResourceBLL.GetText(WebPartId, "UploadUmage", "Upload");
            SaveButton.Click += new EventHandler(SaveButton_Click);

            pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetHeaderFormDialog(ResourceBLL.GetText(WebPartId,"Title","Cập nhật ảnh cho bản tin!"))));
            pl.Controls.Add(labelError);
            pl.Controls.Add(new System.Web.UI.LiteralControl(ResourceBLL.GetText(WebPartId, "Help", "<br><br><span class=cssHelpUploadImageForNews>Chọn file ảnh cần upload hoặc để trắng để xóa ảnh<br>Nếu chỉ upload ảnh nhỏ thì kích vào nút ảnh nhỏ<br>Ảnh lớn kích vào nút upload ảnh lớn<br>Nút upload ở dưới để upload cả 2 ảnh nhỏ và lớn!</span>")));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<table>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl(ResourceBLL.GetText(WebPartId,"FileName","Upload file:")));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<input name=\"txtItemName\" id=\"txtItemName\" type=\"file\" runat=server>"));

            
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td></td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(SaveButton);
            pl.Controls.Add(new System.Web.UI.LiteralControl("<input type=button onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "CloseButton", "Thoát") + "\">"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</table>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetFooterFormDialog()));
        }

     
        void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                string GroupNewsItemId = (string)System.Web.HttpContext.Current.Request["GroupNewsItemId"];
                GroupNewsItemCls GroupNewsItemCls = new GroupNewsBLL().CreateModelItem(Environments.ActiveLanguage, GroupNewsItemId);
                GroupNewsCls GroupNewsCls = new GroupNewsBLL().CreateModel(GroupNewsItemCls.GroupNewsId);
                string belongSite = GroupNewsCls.belongSite;

                labelError.Text = "";
                HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;

                string WorkingPath = Environments.WorkingPath;
                string PathSave = WorkingPath + "\\Media";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\GroupNews";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\GroupNews\\" + GroupNewsCls.GroupNewsId;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\GroupNews\\Images\\" + GroupNewsCls.GroupNewsId;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\GroupNews\\Images\\" + GroupNewsCls.GroupNewsId + "\\" + belongSite;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);


               
                HttpPostedFile hf = (HttpPostedFile)files[0];
                string FileSave = PathSave + "\\" + hf.FileName;
                string UploadName = "";
                if (hf.FileName.Length > 0)
                {
                    hf.SaveAs(FileSave);
                    byte[] b = Environments.GetBytesFromFile(FileSave);
                    Environments.UploadFileWithWs(FileSave, b, WorkingPath);

                    UploadName = new System.IO.FileInfo(FileSave).Name;
                }
                string smallFile = FileSave;
                ServerSideUpload(belongSite, GroupNewsItemId, smallFile);
                labelError.Text = ResourceBLL.GetText(WebPartId,"UploadOk","Upload ảnh minh họa thành công!");
            }
            catch (Exception ex)
            {
                labelError.Text = ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideUpload(string belongSite, string GroupNewsItemId, string smallFile)
        {
            new GroupNewsBLL().UpdateImage(GroupNewsItemId, smallFile);
            return ResourceBLL.GetText(WebPartId,"MessageUploadOk","Upload thành công!");
        }
    }
}